﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.SqlClient;
namespace DataAccess_Layer
{
    public class NhaCungCapDAC
    {

        DataClasses_RetailerDataContext context = DataAccessHelper.DB;
        public int Count()
        {
            var query = (from ncc in context.NhaCungCaps
                         select ncc).Distinct().Count();
            return query;
        }
        public int GetListAllByUser(string tendangnhap)
        {
            var querry = (from ncc in context.NhaCungCaps
                          where ncc.TenDangNhap == tendangnhap
                          select ncc).Distinct().Count();


            return querry;
        }
        public IList<NhaCungCapDTO> GetList(string tendn)
        {
            var query = (from ncc in context.NhaCungCaps
                         where ncc.TenDangNhap == tendn 
                         select new NhaCungCapDTO
                         {
                             ID = ncc.ID,
                             Ten = ncc.Ten,
                             TenDN = ncc.TenDangNhap,
                             WSurl = ncc.WSUrl,
                             TenNH1 = ncc.TenTKNganHang01,
                             SoTK1 = ncc.SoTKNganHang01,
                             TenNH2 = ncc.TenTKNganHang02,
                             SoTk2 = ncc.SoTKNganHang02,
                             SoDT = ncc.SoDT,
                             DiaChi = ncc.DiaChi,
                             MatKhau=ncc.MatKhau,
                             QuocGia = (int)ncc.QuocGia
                         }).Distinct<NhaCungCapDTO>();
            return query.ToList<NhaCungCapDTO>();
        }
        public IList<NhaCungCapDTO> GetList(string tendn, string  pass)
        {
            var query = (from ncc in context.NhaCungCaps
                         where ncc.TenDangNhap == tendn & ncc.MatKhau== pass
                         select new NhaCungCapDTO
                         {
                             ID = ncc.ID,
                             Ten = ncc.Ten,
                             TenDN = ncc.TenDangNhap,
                             WSurl = ncc.WSUrl,
                             TenNH1 = ncc.TenTKNganHang01,
                             SoTK1 = ncc.SoTKNganHang01,
                             TenNH2 = ncc.TenTKNganHang02,
                             SoTk2 = ncc.SoTKNganHang02,
                             SoDT = ncc.SoDT,
                             DiaChi = ncc.DiaChi,
                             QuocGia = (int)ncc.QuocGia,
                             MatKhau=ncc.MatKhau
                         }).Distinct<NhaCungCapDTO>();
            return query.ToList<NhaCungCapDTO>();
        }
        public int GetListAllBySoTK1(string sotk1)
        {
            var querry = (from ncc in context.NhaCungCaps
                          where ncc.SoTKNganHang01 == sotk1
                          select ncc).Distinct().Count();


            return querry;
        }
        public void updateSupper(NhaCungCapDTO ncc)
        {
            var querry = (from nc in context.NhaCungCaps
                          where nc.TenDangNhap == ncc.TenDN
                          select nc).Single();
            querry.ID = ncc.ID;
            querry.Ten = ncc.Ten;
            querry.TenDangNhap = ncc.TenDN;
            querry.DiaChi = ncc.DiaChi;
            querry.MatKhau = ncc.MatKhau;
            querry.SoDT = ncc.SoDT;
            querry.WSUrl = ncc.WSurl;
            querry.SoTKNganHang01 = ncc.SoTK1;
            querry.SoTKNganHang02 = ncc.SoTk2;
            querry.TenTKNganHang01 = ncc.TenNH1;
            querry.TenTKNganHang02 = ncc.TenNH2;
            querry.QuocGia = ncc.QuocGia;

            DataAccessHelper.Save();
        }
        public int GetListAllBySoTK2(string sotk2)
        {
            var querry = (from ncc in context.NhaCungCaps
                          where ncc.SoTKNganHang02 == sotk2
                          select ncc).Distinct().Count();


            return querry;
        }

        public int GetListAllTenncc(string tenncc)
        {
            var querry = (from ncc in context.NhaCungCaps
                          where ncc.Ten == tenncc
                          select ncc).Distinct().Count();


            return querry;
        }
        public int GetListAllBySodt(string sodt)
        {
            var querry = (from ncc in context.NhaCungCaps
                          where ncc.SoDT == sodt
                          select ncc).Distinct().Count();


            return querry;
        }
        public int GetListAll(string tendangnhap, string pass)
        {
            var querry = (from ncc in context.NhaCungCaps
                          where ncc.TenDangNhap == tendangnhap && ncc.MatKhau==pass
                          select ncc).Distinct().Count();


            return querry;
        }
        public void InsertSupplier(NhaCungCap nc)
        {
            NhaCungCap ncc = new NhaCungCap();
            ncc.TenDangNhap = nc.TenDangNhap;
            ncc.MatKhau = nc.MatKhau;
            ncc.WSUrl = nc.WSUrl;
            ncc.Ten = nc.Ten;
            ncc.DiaChi = nc.DiaChi;
            ncc.SoDT = nc.SoDT;
            ncc.TenTKNganHang01 = nc.TenTKNganHang01;
            ncc.SoTKNganHang01 = nc.SoTKNganHang01;
            ncc.TenTKNganHang02 = nc.TenTKNganHang02;
            ncc.SoTKNganHang02 = nc.SoTKNganHang02;
            ncc.QuocGia = nc.QuocGia;
            context.NhaCungCaps.InsertOnSubmit(ncc);
            DataAccessHelper.Save();
        }
        public static bool kiemTraDangNhap(string tenDangNhap, string matKhau)
        {
            DataAccessHelper dah = new DataAccessHelper();
            int kq = (int)dah.ExecuteScalar("Select Count(*) From Supplier Where UserName = @TenDangNhap and Password = @matKhau", "@TenDangNhap", tenDangNhap, "@matKhau", matKhau);
            dah.close();

            return kq == 1;
        }
        public static void delete(List<long> deletedIDs)
        {
            foreach (long deletedID in deletedIDs)
            {
                NhaCungCap tg = DataAccessHelper.DB.NhaCungCaps.Where(t => t.ID == deletedID).Select(t => t).First();
                DataAccessHelper.DB.NhaCungCaps.DeleteOnSubmit(tg);
            }
            DataAccessHelper.Save();
        }
    }
}
